﻿Public Class oPO
#Region "Build Table Structure"
    Private Function BuildTableOPOR() As DataTable
        Dim dt As New DataTable("OPOR")
        dt.Columns.Add("CardCode")
        dt.Columns.Add("DocDate")
        'dt.Columns.Add("DocDueDate")
        dt.Columns.Add("NumAtCard")
        dt.Columns.Add("Comments")
        Return dt
    End Function
    Private Function BuildTablePOR1() As DataTable
        Dim dt As New DataTable("POR1")
        dt.Columns.Add("ItemCode")
        dt.Columns.Add("WhsCode")
        dt.Columns.Add("Quantity", Type.GetType("System.Double"))
        dt.Columns.Add("PriceBefDi", Type.GetType("System.Double"))
        Return dt
    End Function
    Private Function BuildTableSimulateLog() As DataTable
        Dim dt As New DataTable("POR1")
        dt.Columns.Add("ID")
        dt.Columns.Add("ErrMsg")
        Return dt
    End Function
#End Region
#Region "Insert into Table"
    Private Function InsertIntoOPOR(dt As DataTable, dr As DataRow) As DataTable
        Dim drNew As DataRow = dt.NewRow
        drNew("CardCode") = dr("VendorCode")
        drNew("NumAtCard") = dr("VendorReferenceNo")
        drNew("Comments") = dr("Remarks")
        drNew("DocDate") = CDate(dr("PostingDate").ToString).ToString("yyyyMMdd")
        'drNew("DocDueDate") = Now.Date.AddDays(1).ToString("yyyyMMdd")
        'drNew("U_POSTxNo") = PublicVariable.Token
        dt.Rows.Add(drNew)
        Return dt
    End Function
    Private Function InsertIntoPOR1(dt As DataTable, dr As DataRow) As DataTable
        Dim drNew As DataRow = dt.NewRow
        drNew("ItemCode") = dr("ItemCode")
        drNew("WhsCode") = dr("Warehouse")
        drNew("Quantity") = dr("Quantity")
        drNew("PriceBefDi") = 0
        dt.Rows.Add(drNew)
        Return dt
    End Function
    Private Function InsertIntoSimulateLog(dt As DataTable, ID As Integer, ret As String) As DataTable
        Dim drNew As DataRow = dt.NewRow
        drNew("ID") = ID
        drNew("ErrMsg") = ret
        dt.Rows.Add(drNew)
        Return dt
    End Function
#End Region
    
    
    Public Sub CreateAllPO(Simulate As Boolean, DocType As String)
        'Simulate: alway rollback
        Try
            Dim ofn As New Functions
            Dim dt As DataTable = ofn.DoQueryReturnDT("select * from AIPOImpH where Token='" + PublicVariable.Token + "'")
            If dt.Rows.Count = 0 Then Return

            Dim dtErrorLog As DataTable = BuildTableSimulateLog()


            For Each dr As DataRow In dt.Rows
                If Simulate Then
                    If Not PublicVariable.oCompany.InTransaction Then
                        PublicVariable.oCompany.StartTransaction()
                    End If
                End If

                Dim dtPOHeader As DataTable = BuildTableOPOR()
                Dim dtPOLine As DataTable = BuildTablePOR1()

                Dim dt1 As DataTable
                Dim fn As New Functions
                dt1 = fn.DoQueryReturnDT("select * from AIPOImpL where HeaderID=" + dr("ID").ToString)
                Dim ret As String
                dtPOHeader = InsertIntoOPOR(dtPOHeader, dr)
                For Each dr1 As DataRow In dt1.Rows
                    dtPOLine = InsertIntoPOR1(dtPOLine, dr1)
                Next

                Dim ds As New DataSet
                If DocType = "13" Then
                    dtPOHeader.TableName = "OINV"
                    dtPOLine.TableName = "INV1"
                Else
                    dtPOHeader.TableName = "OPCH"
                    dtPOLine.TableName = "PCH1"
                End If
                ds.Tables.Add(dtPOHeader.Copy)
                ds.Tables.Add(dtPOLine.Copy)

                Dim ox As New oXML
                Dim str As String = ox.ToXMLStringFromDS(DocType, ds)
                ret = ox.CreateMarketingDocument(str, DocType)
                If ret = "" Then ret = "Sucessful!"

                dtErrorLog = InsertIntoSimulateLog(dtErrorLog, dr("ID"), ret)
            Next
            If PublicVariable.oCompany.InTransaction Then
                PublicVariable.oCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack)
            End If
            UpdateErrorLog(dtErrorLog)
        Catch ex As Exception
            If PublicVariable.oCompany.InTransaction Then
                PublicVariable.oCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack)
            End If
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Private Sub UpdateErrorLog(dtSimulateLog As DataTable)
        For Each dr As DataRow In dtSimulateLog.Rows
            Dim ors As SAPbobsCOM.Recordset
            ors = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            ors.DoQuery("exec sp_AI_UpdateLog " + dr("ID").ToString + ",'" + dr("ErrMsg").ToString.Replace("'", "") + "'")
        Next
    End Sub
    
End Class
